package ozu.project;

import ozu.project.regions.OzuRegion;
import ozu.project.regions.RegionComponent;

import javax.swing.*;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URI;
import java.util.*;
import java.util.List;


public class RiskBoardComponent extends JComponent implements Observer {

    @Override
    public void paint(Graphics g) {
        final ImageIcon imageIcon = new ImageIcon(new File("C:\\Users\\nuriozalp\\Documents\\NetBeansProjects\\TheRisk\\src\\ozu\\project\\turkiye.jpg").getAbsolutePath());
        Image image = imageIcon.getImage();
        g.drawImage(image, 0, 0, 1000, 500, this);
        super.paint(g);
    }

    private RiskTurkey risk;
    private int height;
    private int width;
    private RegionRelationsComponent relations;
    private Collection<RegionComponent> regionComponents = new ArrayList<>();
    private List<int[]> regionPositions;
    private static final int INIT_HEIGHT = 830;
    private static final int INIT_WIDTH = 1400;
    private static final int[][] REGION_POSITIONS = {{100, 150}, {250, 150}, {50, 400}, {250, 500}, {400, 40},
    {300, 800}, {550, 700}, {150, 800}, {400, 250}, {550, 150}, {550, 400},
    {350, 1280}, {190, 1230}, {100, 1100}};

    public RiskBoardComponent(RiskTurkey risk, int height, int width) {
        this.setLayout(null);
        this.risk = risk;
        this.height = height;
        this.width = width;
        this.relations = new RegionRelationsComponent(risk);
        setRegionPositions();
        addRegions();
        setupRegionComponentRelations();
        this.risk.addObserver(this);
    }

    public RiskBoardComponent(RiskTurkey risk) {
        this(risk, INIT_HEIGHT, INIT_WIDTH);
    }

    @SuppressWarnings("RefusedBequest")
    @Override
    //Override to make it as we want it.
    public Dimension getPreferredSize() {
        return new Dimension(width, height);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.relations.update(g, this.getRegionComponent(risk.getFocused()));
    }

    private void addRegions() {
        for (int i = 0; i < risk.getRegions().length; i++) {
            OzuRegion region = risk.getRegions()[i];
            int[] regionPos = regionPositions.get(i);
            RegionComponent regionComponent = new RegionComponent(region,
                    regionPos[0] * 2 / 3, regionPos[1] * 2 / 3,
                    risk.getRegionController(),
                    risk.getContinentsWhichContains(region)
            );
            //risk.getRegionController().mapRegionToComponent(region, regionComponent);

            this.regionComponents.add(regionComponent);
            this.add(regionComponent);
        }
    }

    public RegionComponent getRegionComponent(OzuRegion region) {
        for (RegionComponent regionComponent : regionComponents) {
            if (regionComponent.getRegion().equals(region)) {
                return regionComponent;
            }
        }
        return null;
    }

    public void setupRegionComponentRelations() {
        this.relations = new RegionRelationsComponent(risk);
        for (OzuRegion region : risk.getRegions()) {
            Iterable<OzuRegion> neighbourRegions = region.getNeighbours();
            RegionComponent regionComponentA = getRegionComponent(region);
            for (OzuRegion neighbourRegion : neighbourRegions) {
                this.relations.addRelation(regionComponentA, getRegionComponent(neighbourRegion));
            }
        }
        this.add(this.relations);
    }

    private void setRegionPositions() {
        this.regionPositions = Arrays.asList(REGION_POSITIONS);
    }

    @Override
    public void update(Observable o, Object arg) {
        this.repaint();
    }
}
